package com.fc.example;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialListener;
import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.anythink.rewardvideo.api.ATRewardVideoListener;

public class RewardVideoActivity extends AppCompatActivity {

    public String mMediaId; // 融合广告位
    ATRewardVideoAd atRewardVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);

        this.mMediaId = "b677607706808a";

        ((TextView) findViewById(R.id.tv_media_id)).setText(getResources().getString(R.string.ad_mediation_id, mMediaId));

        // 加载广告
        this.findViewById(R.id.bt_load).setOnClickListener(it -> loadInterstitialFullAd());

        // 展示广告
        this.findViewById(R.id.bt_show).setOnClickListener(it -> showRewardAd());
    }

    private void loadInterstitialFullAd() {
        Toast.makeText(RewardVideoActivity.this,"开始加载",Toast.LENGTH_SHORT).show();
        atRewardVideoAd = new ATRewardVideoAd(this, mMediaId);
        atRewardVideoAd.setAdListener(new ATRewardVideoListener() {

            @Override
            public void onRewardedVideoAdLoaded() {
                Log.d("test", "onRewardedVideoAdLoaded");
                Toast.makeText(RewardVideoActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdFailed(AdError adError) {
                Log.d("test", "onRewardedVideoAdFailed");
                Toast.makeText(RewardVideoActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdPlayStart(ATAdInfo atAdInfo) {
                Log.d("test", "onRewardedVideoAdPlayStart");
            }

            @Override
            public void onRewardedVideoAdPlayEnd(ATAdInfo atAdInfo) {
                Log.d("test", "onRewardedVideoAdPlayEnd");
            }

            @Override
            public void onRewardedVideoAdPlayFailed(AdError adError, ATAdInfo atAdInfo) {
                Log.d("test", "onRewardedVideoAdPlayFailed");
            }

            @Override
            public void onRewardedVideoAdClosed(ATAdInfo atAdInfo) {
                Log.d("test", "onRewardedVideoAdClosed");
            }

            @Override
            public void onRewardedVideoAdPlayClicked(ATAdInfo atAdInfo) {
                Log.d("test", "onRewardedVideoAdPlayClicked");
            }

            @Override
            public void onReward(ATAdInfo atAdInfo) {
                Log.d("test", "onReward");
            }
        });
        atRewardVideoAd.load();
    }

    // 在加载成功后展示广告
    private void showRewardAd() {
        if (this.atRewardVideoAd == null) {
            Log.d("test", "请先加载广告或等待广告加载完毕后再调用show方法");
            return;
        }
        /** 5、设置展示监听器，展示广告 */
        this.atRewardVideoAd.show(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}